﻿package Server.Poker.NiuNiu;
import net.sf.json.*;

import Server.Agent.GameAgent;
import Server.Model.Base.IRoom;
import Server.Model.Base.SHMUser;
import Server.Model.Hall.QpGameMgr;
import Server.Model.Hall.dthall;
import common.routine.GlobalSetting;
public class niuniuTableMgr {
	int m_LastDealTime;
	int m_LastUpdateTime;

	public static niuniuTable FindTable(int _Uid) {
		try {

			return (niuniuTable) QpGameMgr.getInstance().getUserRoomByUid(_Uid);
		} catch (Exception e) {
		}
		return null;
	}

	public static void Handle_onHeart(int _Uid, String vecArg[]) {
		

	}

public static void Handle_onConnect(GameAgent agent, String[] vecArg) {
		
		SUBCMD_S_Connect cmd = new SUBCMD_S_Connect();
		String sData = cmd.getJsonData();
		agent.SendMessage(sData);
	}

	public static void Handle_onJoin(GameAgent agent, String[] vecArg) {
		SUBCMD_S_Join cmd = new SUBCMD_S_Join();
		String sData = cmd.getJsonData();
		agent.SendMessage(sData);
	}

	public static void Handle_onLeave(GameAgent agent, String[] vecArg) {
		
		Integer _Uid = agent.getUid();
		niuniuTable pTable = (niuniuTable)FindTable(_Uid);
		
		if (pTable != null)
		{
			pTable.OnUserStandUp(_Uid,true);
		}
		QpGameMgr.getInstance().removeUserRoom(_Uid);
		//m_mapHeartTime.remove(_Uid);
		
	}

	public static void Handle_onDismissRoom(GameAgent agent, String[] vecArg) {
		Integer _Uid = agent.getUid();
		niuniuTable tbl = (niuniuTable)FindTable(_Uid);
		if(tbl != null)
		{
			QpGameMgr.getInstance().NotifyRemoveUserRoom(tbl);	
		}
		if(tbl != null && tbl.m_pIUserItem[0].dwUserID == _Uid && tbl.m_bTableStarted == false)
		{
			QpGameMgr.getInstance().NotifyRemoveUserRoom(tbl);	
			tbl.OnEventGameEnd(0, Constants.GAME_END_DISMISS);
		}
	}

	private void SendData(int _Uid, String jsonData) {
		dthall.getInstance().onSend(_Uid, jsonData);
	}

	void Handle_onDismissRoom(int _Uid, String vecArg[]) {

		if (vecArg.length < 2)
			return;

		niuniuTable tbl = FindTable(_Uid);
		if (tbl != null) {
			tbl.OnEventGameEnd(0, Constants.GAME_END_DISMISS);
			tbl.DelayRemove();
			// dthall::instance().OnDismissRoom(tbl);
			if (tbl.ShouldBeRemoved()) {
				// niuniuTableMgr::instance().DismissTable(tbl);
			}
		}

		{
			/*
			 * std::map<long, long>::iterator it = m_mapHeartTime.find(_Uid); if
			 * (it != m_mapHeartTime.end()) { m_mapHeartTime.erase(it); }
			 */
		}

	}

	public static niuniuTable CreateTable(int _Uid, String strParam,boolean isFangGuan) {
		try {
			int byCostGem = 1;
			
			SHMUser user = dthall.getInstance().getUserInfo(_Uid);
			if (user == null)
				return null;

			if (strParam == null)
				return null;

			String[] vecParam = strParam.split(";");

			if (vecParam.length == 0)
				return null;

			int jushu = 0;
			String payway = "AAPAY";
			String betmode = "BETMODE0";
			String zhuangMode = "QIANGZHUANG";
			String lunZhuangRule = "WUNIUXIAZHUANG"; // 轮庄规则
			String xianshiMode = "MINGPAI";
			String guize = "GUIZE_LIMIT3";
			String score_mode = "JINGDIANMOSHI"; // 默认经典模式
			Boolean autoConfigPai = false;
			
			String roomType = "ZUJU";
			
			String strRoomDesc = "";
			
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];

				if (param.equalsIgnoreCase("10JU")) {
					jushu = 10;
//					byCostGem = 10;
				}
				
				if (param.equalsIgnoreCase("20JU")) {
					jushu = 20;
//					byCostGem = 20;
				}
				
				if(param.equalsIgnoreCase("88JU")){
					jushu = 88;//一定是个gold模式
				}
				if (param.equalsIgnoreCase("AAPAY")) {
					byCostGem = jushu * 1;
					payway = "AAPAY";
				}
				
				if (param.equalsIgnoreCase("FZPAY")) {
					byCostGem = jushu * 5;
					payway = "FZPAY";
				}
				if (param.equalsIgnoreCase("WINPAY")) {
					byCostGem = jushu * 5;
					payway = "WINPAY";
				}
				if (param.equalsIgnoreCase("CLUBPAY")){
					payway = "CLUBPAY";
				}
				
				
				if (param.equalsIgnoreCase("DINGZHUANG")) {
					zhuangMode = "DINGZHUANG";
					strRoomDesc += "房主坐庄 ";
				}
				
				if (param.equalsIgnoreCase("LUNZHUANG")) {
					zhuangMode = "LUNZHUANG";
					strRoomDesc += "轮流坐庄 ";
				}
				
				if (-1 != param.indexOf("LUNZHUANG_RULE")) {
					String nameList[] = {
					                     "WUNIUXIAZHUANG",
					                     "NIU1XIAZHUANG",
					                     "NIU2XIAZHUANG",
					                     "NIU3XIAZHUANG",
					                     "LIANGJUXIAZHUANG"
					                     };
					String str = param.substring(param.length() - 1);
					lunZhuangRule = nameList[Integer.valueOf(str)];
				}
				
				if (param.equalsIgnoreCase("QIANGZHUANG")) {
					zhuangMode = "QIANGZHUANG";
					strRoomDesc += "经典抢庄 ";
				}
				
				
				if (param.equalsIgnoreCase("GUIZE_LIMIT3")) {
					guize = "GUIZE_LIMIT3";
				}
				
				if (param.equalsIgnoreCase("GUIZE_LIMIT5")) {
					guize = "GUIZE_LIMIT5";
				}
				
				
				if (param.equalsIgnoreCase("BETMODE0")) {
					betmode = "BETMODE0";
				}
				
				if (param.equalsIgnoreCase("BETMODE1")) {
					betmode = "BETMODE1";
				}
				
				
				if (param.equalsIgnoreCase("AUTOCONFIGPAI")) {
					autoConfigPai = true;
				}
				
				if (param.equalsIgnoreCase("FKMS")) {
					score_mode = "FENGKUANGMOSHI";
				}
				
				if (param.equalsIgnoreCase("JDMS")) {
					score_mode = "JINGDIANMOSHI";
				}
				
				if(param.equalsIgnoreCase("DIZHU")){
					roomType = "GOLD";
				}
			}

			//必须放到DIZHU判断下面
			for(int i = 0;i < vecParam.length;i++){
				String param = vecParam[i];
				if (param.equalsIgnoreCase("DWC")) {
					roomType = "DWC";
					break;
				}
			}
			if(payway.equals("FZPAY") || payway.equals("WINPAY")){
				byCostGem = 25*jushu/5;
			}else{
				byCostGem = 5*jushu/5;
			}
			
			int maxCost = byCostGem;
			if(payway.equals("CLUBPAY")){
				maxCost = 0;
			}
			double dizhu = 0;
			double ruchang = 0;
			double lichang = 0;
			if(roomType.equalsIgnoreCase("GOLD")){
				maxCost = 0;
				byCostGem = 0;
				payway = "GOLD";
				dizhu = Double.parseDouble(vecParam[vecParam.length - 3]);
				ruchang = Double.parseDouble(vecParam[vecParam.length - 2]);
				lichang = Double.parseDouble(vecParam[vecParam.length - 1]);
			}else if(roomType.equalsIgnoreCase("DWC")){
				maxCost = 0;
				byCostGem = 0;
				payway = "DWC";
				dizhu = Double.parseDouble(vecParam[vecParam.length - 3]);
				ruchang = Double.parseDouble(vecParam[vecParam.length - 2]);
				lichang = Double.parseDouble(vecParam[vecParam.length - 1]);
			}
			System.out.println("1[" + maxCost + "]1");
			if (jushu > 0  && byCostGem >= 0 && payway.length() > 0 && user.getGem(0) >= maxCost) {	
				int factor = 0;
				for (int i = 0; i < 1000000; i++) {
					int rand_room = GlobalSetting.getInstance().getRandRoomNum();//Constants.rand(100000, 999999, factor);
					factor = rand_room;
					niuniuTable room = (niuniuTable) QpGameMgr.getInstance().getRoomById(rand_room);
					if (room == null) {
						room = new niuniuTable();
						room.Init(Constants.INIT_TOTAL);
						room.SetParam(strParam);
						room.m_dwTableID = rand_room;
						room.m_byCostGem = byCostGem;
						room.m_payWay = payway;
						room.m_byMaxRound =  jushu;
						room.m_Creator = isFangGuan?_Uid:0;
						room.m_betMode = betmode;
						room.m_scoreMode = score_mode;
						room.m_guize = guize;
						room.m_xianshiMode = xianshiMode;
						room.m_zhuangMode = zhuangMode;
						room.m_lunZhuangRule = lunZhuangRule;
						room.m_bAutoConfigPai = autoConfigPai;
						room.fangzhu = _Uid;
						QpGameMgr.getInstance().putRoomIntoMap(rand_room, room);
						if(roomType.equalsIgnoreCase("GOLD")){
							//设置参数
							room.setBeiShu(dizhu, ruchang, lichang);
							room.setRoomType(roomType);
						}else if(roomType.equalsIgnoreCase("DWC")){
							//设置参数
							room.setBeiShu(dizhu, ruchang, lichang);
							room.setRoomType(roomType);
						}
						return room;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	void DismissTable(niuniuTable pTable) {

		if (pTable == null)
			return;

		/*
		 * {
		 * 
		 * for (std::map<long, niuniuTable>::iterator iter =
		 * m_mapUID2Table.begin(); iter != m_mapUID2Table.end();) { niuniuTable
		 * tbl = iter.second; if (tbl == pTable) { iter =
		 * m_mapUID2Table.erase(iter); } else { iter++; } } }
		 */

		QpGameMgr.getInstance().removeRoomById(pTable.GetID());

	}

	void OnDeal(float dt) {

	}

	public static void OnRecv(GameAgent agent, JSONObject root) {

		String data = root.optString("data");
		String vecArg2[] = data.split("\\|");
		Integer lUid = agent.getUid();

		SHMUser shmUser = dthall.getInstance().getUserInfo(lUid);
		if (shmUser == null)
		{
			return;
		}
		if (vecArg2.length > 0) {
			String subcmd = vecArg2[0];
			switch (subcmd) {
			case "connect":
				Handle_onConnect(agent,vecArg2);
				return;
			case "join":
				Handle_onJoin(agent,vecArg2);
				return;
			case "leave":
				Handle_onLeave(agent,vecArg2);
				return;
			case "dismissroom":
				Handle_onDismissRoom(agent,vecArg2);
				return;			
			default:
				break;
			}
			
			{
				IRoom  iterUser =QpGameMgr.getInstance().getUserRoomByUid(lUid);
				if (iterUser == null)
				{
					return;
				}
				niuniuTable tbl =(niuniuTable) iterUser;
				if (tbl != null)
				{
					if (subcmd.equals("ready")) {
						tbl.Handle_onReady(lUid, vecArg2);
					}
					else if (subcmd.equals("robot")) {
						tbl.Handle_onRobot(lUid, vecArg2);
					}
					else if (subcmd.equals("biaoqing")) {
						tbl.Handle_onBiaoQing(lUid, vecArg2);
					}
					else if (subcmd.equals("yuyin")) {
						tbl.Handle_onYuYin(lUid, vecArg2);
					}
					else if (subcmd.equals("chat")) {
						tbl.Handle_onChat(lUid, vecArg2);
					}
					else if (subcmd.equals("voice")) {
						tbl.Handle_onVoice(lUid, vecArg2);
					} 
					else if (subcmd.equals("voicemsg")) {
						tbl.Handle_onVoiceMsg(lUid, vecArg2);
					}
					else if(subcmd.equals("expr")){
						tbl.Handle_onExpr(lUid, vecArg2);
					} 
					else if (subcmd.equals("try_dismissroom")) {
						tbl.Handle_onTryDismissRoom(lUid, vecArg2);
					}
					else if (subcmd.equals("agree_dismissroom")) {
						tbl.Handle_onAgreeDismissRoom(lUid, vecArg2);
					}
					else if (subcmd.equals("refuse_dismissroom")) {
						tbl.Handle_onRefuseDismissRoom(lUid, vecArg2);
					}
					else if (subcmd.equals("start")) {
						tbl.Handle_onStart(lUid, vecArg2);
					}
					else if (subcmd.equals("qiangzhuang")) {
						tbl.Handle_onQiangZhuang(lUid, vecArg2);
					}
					else if (subcmd.equals("buqiangzhuang")) {
						tbl.Handle_onBuQiangZhuang(lUid, vecArg2);
					}
					else if (subcmd.equals("xiazhu")) {
						tbl.Handle_onXiaZhu(lUid, vecArg2);
					}
					else if (subcmd.equals("xuanniu")) {
						tbl.Handle_onXuanNiu(lUid, vecArg2);
					}
					else if (subcmd.equals("lock")) {
						tbl.Handle_onLock(lUid, vecArg2);
					}else if (subcmd.equals("kick")) {
						tbl.Handle_onKick(lUid, vecArg2);
					}else if (subcmd.equals("inBack")) {
						tbl.Handle_InBack(lUid, vecArg2);
					}
					else if (subcmd.equals("inFore")) {
						tbl.Handle_InFore(lUid, vecArg2);
					}else if(subcmd.equals("gameiap")){
						tbl.Handle_GameIap(lUid, vecArg2);
					}else if(subcmd.equals("quickMatch")){
						tbl.Handle_QuickMatch(lUid, vecArg2);
					}
				}
			}
		}
		
	}
}